﻿using Ewell.Interface.Common.Model;
using Ewell.Interface.Common.Model.Common;
using Ewell.Interface.Common.Model.JsonConverter;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Ewell.Interface.Common.Model.GateWay.Dto
{
    public class MessageHeader:MessageGateWay
    {
        public string @params { get; private set; }
        
        private MessageHeader() { }
        public static MessageHeader CreateDomainParam<T>(T message)
        {
            MessageHeader messageHeader = new MessageHeader();
            MessageHeaderDomain<T> param = new MessageHeaderDomain<T>() { domain = message };
            messageHeader.@params = JsonConvert.SerializeObject(param, new JsonDateTimeConverter());
            return messageHeader;
        }

        public static MessageHeader CreateParam<T>(T message)
        {
            JObject keyValuePairs = JObject.FromObject(new MessageHeaderParamCore());
            var jmsg=JObject.FromObject(message);
            foreach (JProperty kv in jmsg.Properties()) {
                keyValuePairs.Add(kv);
            }
            MessageHeader messageHeader = new MessageHeader();
            messageHeader.@params = JsonConvert.SerializeObject(keyValuePairs,new JsonDateTimeConverter());
            return messageHeader;
        }
    }
    
    public class MessageHeaderDomain<T>: MessageHeaderParamCore
    {
        public T domain { get; set; }
    }

    public class MessageHeaderParamCore
    {
        public User uuheader => LoginInfo.loginUser;
    }
}